package mycoffee.order.jsonhandler;

import com.mobile.android.connectionmanager.HttpRequest;

import mycoffee.order.activity.MyCoffeeApplication;
import mycoffee.order.database.DatabaseConnection;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public abstract class BaseJsonHandler extends Handler {

	public interface OnCompleted {
		public abstract void Finish();
		public abstract void Failed();
	}

	DatabaseConnection dbCon = MyCoffeeApplication.mDatabaseConnection;
	boolean completed;
	boolean mSynz;
	private OnCompleted onCompleted;

	public void setSynz( boolean aIsSynz ) {
		mSynz = aIsSynz;
	}
	
	public boolean isSynz() {
		return mSynz;
	}
	
	public void SetOnCompleted( OnCompleted completed ) {
		this.onCompleted = completed;
	}

	public BaseJsonHandler() {
		completed = false;
	}
	public boolean isCompleted() {
		return completed;
	}
	@Override
	public void handleMessage(Message msg) {
		// TODO Auto-generated method stub
		super.handleMessage(msg);
		switch ( msg.what ) {
		case HttpRequest.SUCCESS:
			String result = "" + msg.obj;
			Log.d("[BaseJsonHandler]", "result = " +result);
			updateData( result );
			this.onCompleted.Finish();
			break;
		case HttpRequest.ERROR:
			processError( HttpRequest.ERROR );
			this.onCompleted.Failed();
			break;
		case HttpRequest.TIMEOUT_REQUEST:
			processError( HttpRequest.TIMEOUT_REQUEST );
			this.onCompleted.Failed();
			break;
		default:
			break;
		}
	}

	private void processError( int error ) {
		switch ( error ) {
			case HttpRequest.ERROR:
				break;
			case HttpRequest.TIMEOUT_REQUEST:
				break;
			default:
				break;
		}
		completed = true;
	}

	protected abstract void updateData(String result);
}
